<?php


namespace App\Services;


use App\Models\AppletsIndexListCategory;
use App\Models\MemberOther;
use App\Models\OrderRiskAlipay;
use App\Models\OtherOrderAllocate;
use Illuminate\Support\Facades\Redis;

class InteractiveRiskQueryService
{
    const url='https://thirdapi.hfnicetech.com/risk/interactiveRiskQuery';//https://digital.jiujiuzu.com
    const clientId='S202401291751792998346051584'; //6eeq3sv40a8ecjtfereafkcur11gw3v0
    const secret='f647317d661b4e92b776b5f8328be549';//qKehHxLY1DbUj-QVGVVeJm

    /**
     * 微信
     * @param $message
     * @param array $request
     * @param array $response
     */
    public static function requestLog($message,$request=[],$response=[]){
        $content = [
            'request'=>is_object($request) ? object_array($request) : $request,
            'response'=>is_object($response) ? object_array($response) : $response,
        ];
        \Illuminate\Support\Facades\Log::debug($message,$content);
    }

    /**
     * 支付宝用户风险识别
     */
    public static function alipayRead($userId,$member_id,$order){
            $url = self::url;
            $bizDataParam=[
                'createAt'=>date('Y-m-d H:i:s',$order['create_time']),
                'idNumber'=>$order['cert_no'],
                'mobile'=>$order['mobile'],
                'name'=>$order['cert_name'],
                'orderId'=>$order['trade_no'],
                'zfbUid'=>$userId,
                'queryType'=>"",
                'customerAppId'=>'2021004105651180',
            ];

            $bizDataParam=json_encode($bizDataParam,JSON_UNESCAPED_UNICODE);
            $encrypted = openssl_encrypt($bizDataParam, 'aes-256-ecb', self::secret, OPENSSL_RAW_DATA, '');
            $encrypted=base64_encode($encrypted);
            $param = [
                'currentPhase'=>'risk_T0',
                'bizData'=>$encrypted,
                'institutionId'=>self::clientId,
            ];

            $result = http_post($url,json_encode($param),['Content-type: application/json;charset=UTF-8']);

            $result = json_decode($result,true);
            self::requestLog('获取支付宝风控报告',$param,$result);
            //成功
            if (isset($result['code']) && $result['success']) {
                $level=$result['result']['riskLevel'];
                $data='未知';
                if($level==998){
                    $data='极⾼危';
                }
                if($level==996){
                    $data='⾼危';
                }
                if($level==979){
                    $data='中风险';
                }
                if($level==975){
                    $data='正常';
                }
                if($level==969){
                    $data='低风险';
                }
                if($level==989){
                    $data='优质';
                }
                OtherOrderAllocate::where('member_id',$member_id)->increment('alipayRiskCount');
                $member=MemberOther::where('user_id',$userId)->first();
                OrderRiskAlipay::setRiskData($member['cert_name'],$member['cert_no'],$member['mobile'],$data,1,$result,$userId,$member_id,$param['reqId']);
            }else{
                $data='未知';
                $member=MemberOther::where('user_id',$userId)->first();
                OrderRiskAlipay::setRiskData($member['cert_name'],$member['cert_no'],$member['mobile'],$data,2,$result,$userId,$member_id,$param['reqId']);
            }
            MemberOther::where('user_id',$userId)->update(['level'=>$data]);
            return $data;

        }





    /**
     * @return void
     * 微签路由查询
     */
    public static function expressRoute(){
        $accessToken = self::getAccessToken();
        if(!$accessToken){
            $flag =  false;
        }else{
            $url = self::url.'/api/express/route';
            $param = [
                'trackingType'=>'1',//1:根据运单号查询,trackingNumber将被当作运单号处理 2:根据客户订单号查询,trackingNumber将被当作客户订单号处理
                'trackingNumber'=>2,//trackingType=1,则此值为运单号 如果trackingType=2,则此值为客户订单号，最多10个单号
            ];
            $accessToken='Bearer '.$accessToken;
            $result = http_post($url,json_encode($param),['Content-type: application/json;charset=UTF-8','Authorization:'.$accessToken]);
            dd($result);
            $result = json_decode($result,true);
            self::requestLog('微签路由查询',$param,$result);
        }
    }

    /**
     * 微签上传文件
     */
    public static function expressUpload(){
        $accessToken = self::getAccessToken();
        if(!$accessToken){
            $flag =  false;
        }else{
            $url = self::url.'/api/express/upload';
            $param = [
                'trackingType'=>'1',//1:根据运单号查询,trackingNumber将被当作运单号处理 2:根据客户订单号查询,trackingNumber将被当作客户订单号处理
                'trackingNumber'=>2,//trackingType=1,则此值为运单号 如果trackingType=2,则此值为客户订单号，最多10个单号
            ];
            $accessToken='Bearer '.$accessToken;
            $result = http_post($url,json_encode($param),['Content-type: application/json;charset=UTF-8','Authorization:'.$accessToken]);
            dd($result);
            $result = json_decode($result,true);
            self::requestLog('微签路由查询',$param,$result);
        }
    }



}
